home *** CD-ROM | disk | FTP | other *** search
- From: Gordon_B@msn.com (Gordon Thomas)
- Subject: Mem Allocation in functions
- Date: 13 Jan 96 01:39:38 -0800
- Message-ID: <00001a80+00006ba7@msn.com>
- Path: news.msn.com!msn.com
- Newsgroups: comp.lang.c
- Organization: The Microsoft Network (msn.com)
-
- I should know the answer, but I don't. I have a function
-
- #include <usual stuff>
- void foo(float *stuff, int *length)
- {
- int i;
- stuff = calloc(*length, sizeof(float));
- // diddle around and put numbers in stuff[i]
- return;
- }
- //
- int main(void)
- {
- int count;
- float *somenums;
- int np = 20;
- void foo(float *, int *);
- foo(somenums,&np);
- for(count = 0 ; count < np ; count++)
- fprintf(stdout,"%4d %10.2f\n",count, somenums[count] ;
- free(somenums);
- return 0;
- }
- My question is: in foo, memory was allocated to the pointer "somenums" and
- at the return the array was filled with the correct values. However,
- in main the array is junk NANs, and in fact the pointer is pointing into DS.
- On the other hand, if "somenums" is calloc'd in main, correct values are
- obtained.
- What language spec am I ignorant of that vitiates the above scheme.
- Thanx
- Gordon
-